package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.constant.UserType;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCardMapper;
import com.ruoyi.system.domain.SysCard;
import com.ruoyi.system.service.ISysCardService;

/**
 * 卡密Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Service
public class SysCardServiceImpl implements ISysCardService 
{
    @Autowired
    private SysCardMapper sysCardMapper;

    /**
     * 查询卡密
     * 
     * @param cardId 卡密主键
     * @return 卡密
     */
    @Override
    public SysCard selectSysCardByCardId(Long cardId)
    {
        return sysCardMapper.selectSysCardByCardId(cardId);
    }

    /**
     * 查询卡密列表
     * 
     * @param sysCard 卡密
     * @return 卡密
     */
    @Override
    public List<SysCard> selectSysCardList(SysCard sysCard)
    {
        return sysCardMapper.selectSysCardList(sysCard);
    }

    /**
     * 新增卡密
     * 
     * @param sysCard 卡密
     * @return 结果
     */
    @Override
    public int insertSysCard(SysCard sysCard)
    {
        sysCard.setCreateTime(DateUtils.getNowDate());
        return sysCardMapper.insertSysCard(sysCard);
    }

    /**
     * 修改卡密
     * 
     * @param sysCard 卡密
     * @return 结果
     */
    @Override
    public int updateSysCard(SysCard sysCard)
    {
        sysCard.setUpdateTime(DateUtils.getNowDate());
        return sysCardMapper.updateSysCard(sysCard);
    }

    /**
     * 批量删除卡密
     * 
     * @param cardIds 需要删除的卡密主键
     * @return 结果
     */
    @Override
    public int deleteSysCardByCardIds(Long[] cardIds)
    {
        return sysCardMapper.deleteSysCardByCardIds(cardIds);
    }

    /**
     * 删除卡密信息
     * 
     * @param cardId 卡密主键
     * @return 结果
     */
    @Override
    public int deleteSysCardByCardId(Long cardId)
    {
        return sysCardMapper.deleteSysCardByCardId(cardId);
    }
    /**
     * 查询卡密  通过卡密 查询卡密实体
     *
     * @param cardno 卡密编码
     * @return 结果
     */
    public  SysCard selectSysCardByCardNo(String cardno) {
        return sysCardMapper.selectSysCardByCardNo(cardno);
    }

    public String createSysCardNo(String ownerid,String cartype,String createBy) throws Exception{
        Date endtime=null;
        if (cartype.equals(UserType.NKHY)||cartype.equals(UserType.ZSHY)){
            //创建Calendar实例
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());   //设置当前时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            ;
            if (cartype .equals(UserType.NKHY)){
                cal.add(Calendar.YEAR, 1);  //在当前时间基础上加一年
                endtime=cal.getTime();
            }else {
                endtime= format.parse("2099-01-01");
            }
        }
        //产生新的uudi
        String carno = UUID.randomUUID().toString().trim().replaceAll("-", "");
        SysCard sysCard=new SysCard();
        if (StringUtils.isNotEmpty(ownerid)){
            sysCard.setOwnerid(ownerid);
        }else {
            sysCard.setOwnerid("1");
        }
        sysCard.setCardNo(carno);
        sysCard.setCardType(cartype);
        sysCard.setStarttime(new Date());
        sysCard.setEndtime(endtime);
        sysCard.setCreateBy(createBy);
        sysCard.setCreateTime(new Date());
        sysCard.setStatus(0);//设置为代使用
       insertSysCard(sysCard);
       return  carno;
    }
}
